﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using TERP.WaterClient.Common;

namespace TERP.WaterClient.Entities.Auth
{
	public class FunctionCollection : List<FunctionObject>
	{
		private DataTable _dtbData;

		public FunctionCollection()
            : base()
		{
        }

		/// <summary>
		/// 
		/// </summary>
		public DataTable DataTableFunction
		{
			get
			{
				return _dtbData;
			}
			set {
				_dtbData = value;
			}
		}

        /// <summary>
        /// 
        /// </summary>
        public void ClearData()
        {
            this.Clear();
            _dtbData = null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="functionCode"></param>
        /// <returns></returns>
        public bool ContainsFunctionCode (string functionCode){
            foreach (FunctionObject fnc in this){
                if (fnc.ObjectCode.Equals(functionCode))
                    return true;
            }
            return false;
        }

		/// <summary>
		/// 
		/// </summary>
		public void FillToDataTable()
		{
			_dtbData = new DataTable(DbTable.Tbl_Function_Object);
			_dtbData.Columns.AddRange(new DataColumn[] { 
		        new DataColumn(TblFunctionObject.Object_Code,typeof(string)),
				new DataColumn(TblFunctionObject.Object_Name,typeof(string)),
				new DataColumn(TblFunctionObject.Object_Code_Parent,typeof(string)),
				new DataColumn(TblFunctionObject.Object_Code_Exec,typeof(string)),
				new DataColumn(TblFunctionObject.Object_Type,typeof(string)),
				new DataColumn(TblFunctionObject.Partner_Code,typeof(string)),
				new DataColumn(TblFunctionObject.Flag_Active,typeof(string))
			});
			DataRow dtRow;
			foreach (FunctionObject grp in this)
			{
				dtRow = _dtbData.NewRow();
				dtRow[TblFunctionObject.Object_Code] = grp.ObjectCode;
				dtRow[TblFunctionObject.Object_Name] = grp.ObjectName;
				dtRow[TblFunctionObject.Object_Code_Parent] = grp.ObjectCodeParent;
				dtRow[TblFunctionObject.Object_Code_Exec] = grp.ObjectCodeExec;
				dtRow[TblFunctionObject.Object_Type] = grp.ObjectType;
				dtRow[TblFunctionObject.Partner_Code] = grp.PartnerCode;
				dtRow[TblFunctionObject.Flag_Active] = grp.FlagActive;
				//
				_dtbData.Rows.Add(dtRow);
			}
			_dtbData.AcceptChanges();
		}
	}
}
